package org.eweb4j.mvc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.eweb4j.mvc.action.ActionProp;
import org.eweb4j.mvc.action.QueryParams;
import org.eweb4j.mvc.config.bean.ActionConfigBean;

public class Context {
	private ServletContext servletContext;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private HttpSession session;
	private PrintWriter writer;
	private ServletOutputStream out;
	private String httpMethod;
	private ActionProp actionProp;
	private Map<String, String[]> queryParamMap;
	private Map<String, String[]> pathParamMap;
	private ActionConfigBean mvcBean;
	private String uri;
	private QueryParams queryParams;

	public Context() {
	}

	public Context(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response,
			HttpSession session, PrintWriter writer, ServletOutputStream out,
			String httpMethod) {
		super();
		this.servletContext = servletContext;
		this.request = request;
		this.response = response;
		this.session = session;
		this.writer = writer;
		this.out = out;
		this.httpMethod = httpMethod;
	}

	public Context(ServletContext servletContext, HttpServletRequest request, HttpServletResponse response,
			HttpSession session, PrintWriter writer, ServletOutputStream out,
			String httpMethod, ActionProp actionProp,
			Map<String, String[]> queryParamMap,
			Map<String, String[]> pathParamMap, ActionConfigBean mvcBean,
			String uri, QueryParams queryParams) {
		super();
		this.servletContext = servletContext;
		this.request = request;
		this.response = response;
		this.session = session;
		this.writer = writer;
		this.out = out;
		this.httpMethod = httpMethod;
		this.actionProp = actionProp;
		this.queryParamMap = queryParamMap;
		this.pathParamMap = pathParamMap;
		this.mvcBean = mvcBean;
		this.uri = uri;
		this.queryParams = queryParams;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public ServletContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		if (session == null)
			session = request.getSession();

		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public PrintWriter getWriter() throws IOException {
		if (writer == null)
			writer = response.getWriter();

		return writer;
	}

	public void setWriter(PrintWriter writer) {
		this.writer = writer;
	}

	public ServletOutputStream getOut() throws IOException {
		if (out == null)
			out = response.getOutputStream();

		return out;
	}

	public void setOut(ServletOutputStream out) {
		this.out = out;
	}

	public String getHttpMethod() {
		return httpMethod;
	}

	public void setHttpMethod(String httpMethod) {
		this.httpMethod = httpMethod;
	}

	public ActionProp getActionProp() {
		return actionProp;
	}

	public void setActionProp(ActionProp actionProp) {
		this.actionProp = actionProp;
	}

	public Map<String, String[]> getQueryParamMap() {
		if (queryParamMap == null)
			queryParamMap = new HashMap<String, String[]>();

		return queryParamMap;
	}

	public void setQueryParamMap(Map<String, String[]> queryParamMap) {
		this.queryParamMap = queryParamMap;
	}

	public Map<String, String[]> getPathParamMap() {
		if (pathParamMap == null)
			pathParamMap = new HashMap<String, String[]>();

		return pathParamMap;
	}

	public void setPathParamMap(Map<String, String[]> pathParamMap) {
		this.pathParamMap = pathParamMap;
	}

	public ActionConfigBean getMvcBean() {
		return mvcBean;
	}

	public void setMvcBean(ActionConfigBean mvcBean) {
		this.mvcBean = mvcBean;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public QueryParams getQueryParams() {
		if (queryParams == null) {
			queryParams = new QueryParams();
			queryParams.setMap(queryParamMap);
		}

		return queryParams;
	}

	public void setQueryParams(QueryParams queryParams) {
		this.queryParams = queryParams;
	}

}
